var vm = new Vue({
    el:'#wrapper',
    data(){
        return{
            list:[{
                "teacherId": "",
                "teacherName": "",
                "courseName":"暂未给当前老师安排课程！",
                "isAdmin": "",
                "isDelete": "",
                "classId": "",
                "sex": 0,
                "password": 5000,
                "phone": 2,
                "posts": "",
                "salary": "",
            }],
            current:1,
            rolee : "",
            number:"",
            fileList: [{
                name: '',
                url: ''
                }]
        }
    },
    methods:{
        deleteTeacher(item){
            var $this = this;
            $.ajax({
                url:"/teacher/delete",
                type:"get",
                data:{"teacherId":item},
                async:false,
                success(data){
                    $this.$message.success("删除成功！");
                    var current = $("#current").text();
                    getPage(current);
                },
                error(){
                    $this.$message.error("删除失败！");
                }
            })
        },
        updateStatus(item,status){
            var $this = this;
            if(item.teacherName === "暂时未安排老师！"){
                $this.$message.error("课程未安排老师，不能启用！");
                return;
            }
            $.ajax({
                url:"/course/updateStatus",
                dataType: "json",
                type:"put",
                data:{"courseId":item.courseId,"status":status},
                async: "false",
                success(data) {
                    $this.$message.success("状态修改成功！");
                    getPage($this.current)
                },
                error() {
                    $this.$message.error("状态修改失败！");
                }

            })
        },
        jump(){
            var current = $("#page").val();
            if(parseInt(current) < 1 || parseInt(current) >= parseInt(lastPage)+1 || parseInt(current) == NaN
                || parseInt(current) == undefined){
                vm.$message.error("你输入的数字不在范围内！");
                return
            }
            getPage(current);
        },
        toUpdate(courseId){
            location.href = "user-message.html?updateTeacherId="+courseId;
        },
        toAdd(){
            location.href = "user-message.html?addTeacher=true";
        },
        importData(){
            $("#fileForm").submit();

        }


    }
});

$(function (){
    var url = location.search;
    if(url.indexOf("upload=true")!=-1){
        console.log("导入成功")
        vm.$message.success("导入成功")
    }

    getPage(vm.current);
    var lastPage = $("#lastPage").text();
    $("#homePage").click(function (){
        getPage(1);
    })
    $("#backPage").click(function (){
        getPage(lastPage);
    })
    $("#next").click(function (){

        lastPage = $("#lastPage").text();
        vm.current++;
        if(vm.current > parseInt(lastPage)){
            vm.current = lastPage;
            return;
        }
        getPage(vm.current);
    })
    $("#prev").click(function (){
        vm.current--;
        if(vm.current < 1){
            vm.current = 1;
            return
        }
        getPage(vm.current);
    })
    $("#teacherSearch").click(function (){
        getPage(vm.current);
    })
    //setInterval("getPage(vm.current)",1000)
})

function getPage(current){
    var teacherId = $("#teacherId").val();
    var teacherName = $("#teacherName").val();
    var $this = this;
    $.ajax({
        url:"/teacher/page",
        dataType:"json",
        type:"get",
        data: {"current":current,"size":8,"teacherId":teacherId,"teacherName":teacherName},
        async:false,
        success(data){
            vm.list = data.data.teacherList.records;
            vm.current = data.data.teacherList.current;
            $("#current").html(" &nbsp;"+data.data.teacherList.current+"&nbsp; ");
            $("#lastPage").html(data.data.teacherList.pages);
        },
        error(){
            vm.$message.error("数据获取失败!");
        }
    })



}

